package com.moli.iov.down.command.design.command.reshandler.impl;

import com.moli.iov.down.command.codec.decode.bean.RemoteControlResResultBean;
import com.moli.iov.down.command.codec.decode.decoders.RemoteControlDecoder;
import com.moli.iov.down.command.codec.utils.Protocol;
import com.moli.iov.down.command.design.command.reshandler.ResponseHandlerCommand;
import com.moli.iov.down.command.mq.SendCache;
import com.moli.iov.down.command.notice.CommandNotice;
import com.moli.iov.down.enums.OperatorResultEnum;
import com.moli.iov.down.enums.command.RemoteControlResponseEnum;
import com.moli.iov.down.enums.command.RemoteControlResultEnum;
import com.moli.iov.down.model.po.VehicleCommand;
import com.moli.iov.down.service.IVehicleCommandService;
import com.moli.iov.util.SpringContextUtils;
import java.util.Date;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * <p>
 * 远程车辆控制命令结果应答
 * </p>
 *
 * @author: zql
 * @Date: 2018/12/29 17:29
 */
public class RemoteControlResultCommand implements ResponseHandlerCommand {

    protected final Logger logger = LoggerFactory.getLogger(this.getClass());

    private SendCache sendCache = SpringContextUtils.getBean("sendCache");

    private IVehicleCommandService vehicleCommandService =
                        SpringContextUtils.getBean("vehicleCommandService");

    private CommandNotice commandNotice =
                        SpringContextUtils.getBean("commandNotice");

    @Override
    public void execute(Protocol protocol) {
        String vin = protocol.getVin();
        byte command = protocol.getCommandTag();

        //远程车辆控制命令结果应答 业务处理
        RemoteControlDecoder remoteControlDecoder = SpringContextUtils.getBean("remoteControlDecoder");
        RemoteControlResResultBean decode = remoteControlDecoder.decode(protocol.getData());
        decode.setVin(vin);
        String serialNumber = String.valueOf(decode.getSerialNumber());

        this.remoteControlResultHandle(decode);

        sendCache.updateResResult(vin, command, serialNumber);

        notifyOther(decode);
    }


    /**
     * 远程控制结果响应 handle
     *
     * @param decode
     */
    public void remoteControlResultHandle(RemoteControlResResultBean decode){
        logger.info("[上行响应报文] [远程控制响应] handle remoteControlResBean：{}", decode);

        VehicleCommand vehicleCommand = new VehicleCommand();
        vehicleCommand.setVin(decode.getVin());
        vehicleCommand.setResponseNo(Long.parseLong(String.valueOf(decode.getSerialNumber())));
        vehicleCommand.setResponseTime(new Date());

        vehicleCommand.setResult(RemoteControlResultEnum.SUCCESS.getFlag() == decode.getResult() ?
                OperatorResultEnum.SUCCESS.getFlag() : OperatorResultEnum.FAIL.getFlag());
        if(RemoteControlResultEnum.FAIL.getFlag() == decode.getResult()){
            vehicleCommand.setResultMemo(RemoteControlResponseEnum.getMsg(decode.getFailCode()));
        }

        vehicleCommandService.updateResult(vehicleCommand);
    }

    /**
     * 操作结果通知其它服务
     * @param decode
     */
    public void notifyOther(RemoteControlResResultBean decode){
        if(RemoteControlResultEnum.SUCCESS.getFlag() != decode.getResult()){
            return;
        }
        commandNotice.trigger(decode.getVin(), Long.parseLong(String.valueOf(decode.getSerialNumber())));
        logger.info("操作结果通知其它服务 vin:{} SerialNumber:{}", decode.getVin(), decode.getSerialNumber());

    }

}
